面试题 17.14. Smallest K LCCI

1. Question

Design an algorithm to find the smallest K numbers in an array.

2. Examples

Input:  arr = [1,3,5,7,2,4,6,8], k = 4
Output:  [1,2,3,4]

3. Constraints

  • 0 <= len(arr) <= 100000
  • 0 <= k <= min(100000, len(arr))

4. References

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/smallest-k-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

5. Solutions

class Solution {
    public int[] smallestK(int[] arr, int k) {
        return Arrays.stream(arr).sorted().limit(k).toArray();
    }
}
Copyright © rootwhois.cn 2021-2022 all right reserved,powered by GitbookFile Modify: 2023-03-05 10:55:52

results matching ""

    No results matching ""